/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaServices;

import entities.Favorites;
import entities.MyBatisUtil;
import java.util.List;
import org.apache.ibatis.session.SqlSession;

/**
 *
 * @author michael
 */
public class FavoritesManager {
    
    public static void insertUser(Favorites user) {
	  SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
	  try{
	  sqlSession.insert("mapper.FavoritesMapper.insertUser", user);	  
	  sqlSession.commit();
	  }finally{
	   sqlSession.close();
	  }
	 }
	 
	 public static Favorites getUserById(int userId) {
	  SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
	  try{
	  Favorites userMapper = sqlSession.selectOne("mapper.FavoritesMapper.getById", userId);
	  return userMapper;
	  }finally{
	   sqlSession.close();
	  }
	 }
	 
         public static Favorites getUserByEmail(String Username) {           
             
	  SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
	  try{
	  Favorites userMapper = sqlSession.selectOne("mapper.FavoritesMapper.getUserByEmail", Username);
	  return userMapper;
	  }finally{
	   sqlSession.close();
	  }
	 }
         
	 public static List<Favorites> getAllUsers() {
	  SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
	  try{
	  List<Favorites> userMapper = sqlSession.selectList("mapper.FavoritesMapper.getAll");
	  return userMapper;
	  }finally{
	   sqlSession.close();
	  }
	 }
	 
	 public static void updateUser(Favorites user) {
	  SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
	  try{
	  sqlSession.update("mapper.FavoritesMapper.updateUser", user);	  
	  sqlSession.commit();
	  }finally{
	   sqlSession.close();
	  }
	   
	 }
	 
	 public static void deleteUser(int userId) {
	  SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
	  try{
	  sqlSession.update("mapper.FavoritesMapper.deleteUser", userId);	  
	  sqlSession.commit();
	  }finally{
	   sqlSession.close();
	  }
         }
    
}
